package com.example.testcontract.fragments;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.testcontract.R;
import com.example.testcontract.adapter.DialAdapter;
import com.example.testcontract.bean.Dial;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2014/9/18.
 */
public class DialFragment extends Fragment implements AbsListView.OnScrollListener,LoaderManager.LoaderCallbacks<Cursor> {
    private ListView dialRecordListView;
    private DialAdapter adapter;
    private List<Dial> dialList = new ArrayList<Dial>();

    public static DialFragment newInstance() {
        return new DialFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dial, container, false);
        dialRecordListView = (ListView) rootView.findViewById(R.id.dial_list_view);
        getLoaderManager().initLoader(1000, null, this);
//        adapter = new DialAdapter(getActivity(), R.layout.dial_item, dialList);
//
//        Uri uri = CallLog.Calls.CONTENT_URI;
//        Cursor cursor = getActivity().getContentResolver().query(uri, new String[]{
//                CallLog.Calls.CACHED_NAME,                 //通话人姓名
//                CallLog.Calls.NUMBER,                        //通话人电话号码
//                CallLog.Calls.TYPE,                         // 通话类型
//                CallLog.Calls.DATE,                         // 通话时间
//                CallLog.Calls.DURATION                     // 通话时长
//        }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
//        if (cursor.moveToFirst()) {
//            do {
//                String name = cursor.getString(0);
//                String phoneNum = cursor.getString(1);
//                int type = cursor.getInt(2);
//                String str_type = "";
//                if (type == CallLog.Calls.INCOMING_TYPE) {
//                    str_type = "呼入";
//                } else if (type == CallLog.Calls.OUTGOING_TYPE) {
//                    str_type = "呼出";
//                } else if (type == CallLog.Calls.MISSED_TYPE) {
//                    str_type = "未接";
//                }
//                Date date = new Date(Long.parseLong(cursor.getString(3)));
//                long duration = cursor.getLong(4);
//
//                Dial dialRecord = new Dial();
//                dialRecord.setName(name);
//                dialRecord.setPhoneNumber(phoneNum);
//                dialRecord.setState(str_type);
//                dialRecord.setDate(date);
//                dialRecord.setDuration(duration);
//                dialList.add(dialRecord);
//            }while (cursor.moveToNext());
//        }
//        getActivity().startManagingCursor(cursor);
//
//        dialRecordListView.setAdapter(adapter);
        return rootView;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (totalItemCount - firstVisibleItem < 15) {
            Log.d("dial list view", "加载20条数据");
            adapter.setCount(adapter.getCount() + 20);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        adapter = new DialAdapter(getActivity(), R.layout.dial_item, dialList);
        CursorLoader cursorLoader = new CursorLoader(getActivity());
        cursorLoader.setUri(CallLog.Calls.CONTENT_URI);
        cursorLoader.setProjection(new String[]{
                CallLog.Calls.CACHED_NAME,                 //通话人姓名
                CallLog.Calls.NUMBER,                        //通话人电话号码
                CallLog.Calls.TYPE,                         // 通话类型
                CallLog.Calls.DATE,                         // 通话时间
                CallLog.Calls.DURATION                     // 通话时长
        });
        cursorLoader.setSortOrder(CallLog.Calls.DEFAULT_SORT_ORDER);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(0);
                String phoneNum = cursor.getString(1);
                int type = cursor.getInt(2);
                String str_type = "";
                if (type == CallLog.Calls.INCOMING_TYPE) {
                    str_type = "呼入";
                } else if (type == CallLog.Calls.OUTGOING_TYPE) {
                    str_type = "呼出";
                } else if (type == CallLog.Calls.MISSED_TYPE) {
                    str_type = "未接";
                }
                Date date = new Date(Long.parseLong(cursor.getString(3)));
                long duration = cursor.getLong(4);

                Dial dialRecord = new Dial();
                dialRecord.setName(name);
                dialRecord.setPhoneNumber(phoneNum);
                dialRecord.setState(str_type);
                dialRecord.setDate(date);
                dialRecord.setDuration(duration);
                dialList.add(dialRecord);
            } while (cursor.moveToNext());
        }  else {
            Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
            return;
        }
        dialRecordListView.setAdapter(adapter);
        dialRecordListView.setOnScrollListener(this);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }
}
